package com.spsyry.pay.order;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCNqJ71yMxSdVgU3Vo/3yiMe47n8M4hIamekOBqI7zMNq1hq2sjGh5zR0/8fVPrBIaFPQvqTgj2Ola+oBI2hzibO0wYfGA9kAfDlFjGslraJ0ijcvKO/o07zM42YBJqlESgEmF76+E3qA04DcBR+xSvOfeuF8Y49xV7x27drv2Mfkj1HtWbA4HcQOPGYWpSUu+78TWtnSqRWGvFD4DwAa3fL08yakOIbh+bJsUSSfFCG2XY0IHQOwkoRBDFspNvGkbL24eLm17tM2OxtlfYV4eENStFTvdETyFrUUMy+N/TenwsUzVhasYgnpowjG02kkYibworbvDQOpDXoiqSR2KdAgMBAAECggEAZBzDGpiqLaxYb4Zv/avMkFvSBwEb7DZKFZH1x40yJHCP3/KxaMFQu8+uK5a28+Ti3M7j2NcFZl6cZ2Pg+0BdNPeXM8bSjS95DTON+f6aTopsv3bBDWXAkn6PTcZfvYrDZQ0Yb+en5xmRHgg53rY/TAb/j72ljSSxviSXqXu+HMclwhyTqq3jWf4dr6eqCfsix/w6lHMygJuqNwN8jAhbxIVBhZV4YwMmfbTBO5q+5Z80XAPHWQcyybzU8jKjjzo5ZpB68hYAKIHeeTFMEpOH2zcrfH5XGBaY9rNNG1c/nxlMdJnZfcdFXIujOqqeAEMaaqXpJgv1FMBzr4xA9Tw1UQKBgQDAWbjnba5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fGOPcVe8du3a79jH5I9R7VmwOB3EDjxmFqUlLvu/E1rZ0qkVhrxQ+A8AGt3y9PMmpDiG4fmybFEknxQhtl2NCB0DsJKEQQxbKTbxpGy9uHi5te7TNjsbZX2FeHhDUrRU73RE8ha1FDMvjf03p8LFM1YWrGIJ6aMIxtNpJGIm8KK27w0DqQ16IqkkdinQIDAQAB";

    public static final String OUT_TRADE_NO = "2002345679920129";

    //生成支付订单接口
    public Map pay() throws AlipayApiException {
    //public Map pay(PayData payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000149666087");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        /*model.setOutTradeNo(payData.getCode());
        model.setTotalAmount(payData.getMoney().toString());
        model.setSubject(payData.getName());*/

        model.setOutTradeNo(OUT_TRADE_NO);
        model.setTotalAmount("0.01");
        model.setSubject("测试产品");

        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);

        //后台配置文件不行
        //request.setNotifyUrl("https://71a6-61-181-211-208.ngrok-free.app/alipay");

        //回调地址
        request.setNotifyUrl("https://545d-2409-8a02-241c-b100-9c5e-b736-82a2-d228.ngrok-free.app/alipay/notify");




        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);

        System.out.println("==============================");
        System.out.println(response.getBody());
        System.out.println("===============================");

        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000133628817");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }


     public static void main(String[] args) {
        try {
            ZhifubaoUtill utill = new ZhifubaoUtill();
            utill.pay();
            //utill.queryResult(OUT_TRADE_NO);
            Thread.sleep(1000);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
